<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['report:record:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['report:record:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['report:record:export']">导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" class="el-form-search">
        <!--设备选择-->
        <el-form-item label="设备" porp="device" class="el-form-search-item">
          <el-select v-model="queryParams.device" placeholder="请选择设备类型" clearable filterable @change="setChannelList">
            <el-option v-for="device in deviceList" :key="device.zhGuid" :label="device.devName"
              :value="device.zhGuid" />
          </el-select>
        </el-form-item>
        <!--通道选择-->
        <el-form-item label="通道" porp="tdGuid" class="el-form-search-item">
          <el-select v-model="queryParams.tdGuid" placeholder="请选择设备类型" clearable filterable>
            <el-option v-for="channel in channelList" :key="channel.tdGuid" :label="channel.channelName"
              :value="channel.tdGuid" />
          </el-select>
        </el-form-item>
        <!--数据类型-->
        <el-form-item label="数据类型" porp="selectedTable" class="el-form-search-item">
          <el-select v-model="queryParams.selectedTable" placeholder="请选择数据类型">
            <el-option v-for="table in tables" :key="table.value" :label="table.label" :value="table.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="时间" porp="timeRange" class="el-form-search-item">
          <!-- 时间选择 -->
          <el-date-picker v-model="queryParams.timeRange" type="datetimerange" range-separator="至"
            start-placeholder="开始时间" end-placeholder="结束时间" style="width: 400px" value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <!-- 查询按钮 -->

        <el-form-item class="el-form-search-item">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置
          </el-button>
        </el-form-item>

      </el-form>
    </el-row>

    <el-table border :height="tableHeight" v-loading="loading" :data="recordList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="50" align="center">
        <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="sTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="eTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通道GUID" align="center" prop="tdGuid" />
      <el-table-column label="事件记录时间" align="center" prop="timeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据类型" align="center" prop="waveType" />
      <el-table-column label="实际波形数据" align="center" prop="wave" />
      <el-table-column label="事件路径" align="center" prop="path" />
      <el-table-column label="文件标志" align="center" prop="mark" />
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="特征相位" align="center" prop="iPase" />
      <el-table-column label="触发相位" align="center" prop="nPase" />
      <el-table-column label="持续时间(ms)" align="center" prop="keepTime" />
      <el-table-column label="特征值" align="center" prop="evtVal" />
      <el-table-column label="A相电压" align="center" prop="voltA" />
      <el-table-column label="B相电压" align="center" prop="voltB" />
      <el-table-column label="C相电压" align="center" prop="voltC" />
      <el-table-column label="额定电压" align="center" prop="eddy" />
      <el-table-column label="规约类型" align="center" prop="gylx" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['report:record:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['report:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改record对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false"
      v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="通道GUID" prop="tdGuid">
          <el-input v-model="form.tdGuid" placeholder="请输入通道GUID" />
        </el-form-item>
        <el-form-item label="事件记录时间" prop="timeDate">
          <el-date-picker clearable size="mini" v-model="form.timeDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择事件记录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="事件路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入事件路径" />
        </el-form-item>
        <el-form-item label="文件标志" prop="mark">
          <el-input v-model="form.mark" placeholder="请输入文件标志" />
        </el-form-item>
        <el-form-item label="特征相位" prop="iPase">
          <el-input v-model="form.iPase" placeholder="请输入特征相位" />
        </el-form-item>
        <el-form-item label="触发相位" prop="nPase">
          <el-input v-model="form.nPase" placeholder="请输入触发相位" />
        </el-form-item>
        <el-form-item label="开始时间" prop="sTime">
          <el-date-picker clearable size="mini" v-model="form.sTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="eTime">
          <el-date-picker clearable size="mini" v-model="form.eTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="持续时间(ms)" prop="keepTime">
          <el-input v-model="form.keepTime" placeholder="请输入持续时间(ms)" />
        </el-form-item>
        <el-form-item label="特征值" prop="evtVal">
          <el-input v-model="form.evtVal" placeholder="请输入特征值" />
        </el-form-item>
        <el-form-item label="A相电压" prop="voltA">
          <el-input v-model="form.voltA" placeholder="请输入A相电压" />
        </el-form-item>
        <el-form-item label="B相电压" prop="voltB">
          <el-input v-model="form.voltB" placeholder="请输入B相电压" />
        </el-form-item>
        <el-form-item label="C相电压" prop="voltC">
          <el-input v-model="form.voltC" placeholder="请输入C相电压" />
        </el-form-item>
        <el-form-item label="额定电压" prop="eddy">
          <el-input v-model="form.eddy" placeholder="请输入额定电压" />
        </el-form-item>
        <el-form-item label="规约类型" prop="gylx">
          <el-input v-model="form.gylx" placeholder="请输入规约类型" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/report/record";
import { getDeviceListFold } from '@/api/device/device'
export default {
  name: "Record",
  data() {
    return {
      // 表格高度
      tableHeight: "calc(100vh - 200px)",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // record表格数据
      recordList: [],
      deviceList: [],
      channelList: [],
      tables: [
        { value: 'base', label: '原始数据' },
        { value: 'avg', label: '平均值' },
        { value: 'max', label: '最大值' },
        { value: 'min', label: '最小值' }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        orderByColumn: "create_time",
        device: "",
        timeRange: [],
        selectedTable: null,
        tdGuid: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceList()
  },
  watch: {
    queryParams: {
      handler(newForm, oldForm) {
        let obj = Object.assign({}, newForm)
        obj.device = obj.deviceId
        delete obj.deviceId
        this.$store.commit('UPDATE_FILTER_CONDITIONS', obj)
      }, deep: true
    }
  },
  methods: {


    getDeviceList() {
      getDeviceListFold().then((res) => {
        console.log(res)
        this.deviceList = res.rows
        if (this.queryParams.device !== null || this.queryParams.device !== "") {
          this.channelList = this.deviceList.filter((item) => {
            return item.zhGuid === this.queryParams.device
          })[0].channelList
        }
      })
    },
    setChannelList(selectedValue) {
      console.log(selectedValue)
      console.log(this.deviceList)

      this.channelList = this.deviceList.filter((item) => {
        return item.zhGuid === selectedValue
      })[0].channelList
      this.queryParams.channel = this.channelList[0].tdGuid
    },
    /** 查询record列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tdGuid: null,
        timeDate: null,
        waveType: null,
        wave: null,
        path: null,
        mark: null,
        eventType: null,
        iPase: null,
        nPase: null,
        sTime: null,
        eTime: null,
        keepTime: null,
        evtVal: null,
        voltA: null,
        voltB: null,
        voltC: null,
        eddy: null,
        gylx: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加record";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改record";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = [row.id] || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
